// ignore_for_file: constant_identifier_names

import 'package:html/dom.dart' as dom;

extension MyLoopElement on List<dom.Element> {
  loopAction(void Function(dom.Element ele) action) {
    for (var element in this) {
      action(element);
    }
  }

  loopActionIndex(void Function(dom.Element ele, int index) action) {
    for (var i = 0; i < length; i++) {
      var element = this[i];
      action(element, i);
    }
  }
}

extension MyMapLoad on Map<String, dynamic> {
  String toValue(String key, [String? lable]) {
    var v = this[key];
    if (v == null) return "";
    if (v is String && v.isEmpty) return "";
    lable ??= key;
    return "&$lable=$v";
  }

  String toValue1(String key) {
    var v = this[key];
    if (v == null) return "";
    if (v is String && v.isEmpty) return "";
    return "$v";
  }
}

extension MyParseInt on String? {
  int toInt() {
    return int.tryParse(this ?? "") ?? 0;
  }
}

extension MyParseElement on dom.Element {
  dom.Element? loadClassName(String className) {
    if (className.startsWith(".")) {
      var arr = className.substring(1).split(".");
      var list = getElementsByClassName(arr[0]);
      for (var item in list) {
        var itemClassName = item.className;
        var hadAll = true;
        for (var child in arr) {
          if (!itemClassName.contains(child)) {
            hadAll = false;
            break;
          }
        }
        if (hadAll) {
          return item;
        }
      }
      return null;
    } else {
      var list = getElementsByClassName(className);
      if (list.isEmpty) {
        return null;
      }
      return list[0];
    }
  }

  dom.Element? loadTagClassName(String name) {
    var arr = name.split('.');
    String tag = arr[0];
    String className = arr[1];
    var list = getElementsByClassName(className);
    if (list.isEmpty) {
      return null;
    }
    var ele = list.firstWhere(
      (element) => element.localName == tag,
      orElse: () => dom.Element.tag('empty'),
    );
    if (ele.localName == 'empty') return null;
    return ele;
  }

  List<dom.Element>? loadTagClassNames(String name1) {
    String name = name1.trim();
    if (name.trim().isEmpty) return null;

    if (name.startsWith(".")) {
      return getElementsByClassName(name);
    }
    if (!name.contains(".")) {
      return getElementsByTagName(name);
    }
    var arr = name.split('.');
    String tag = arr[0];
    String className = arr[1];
    var list = getElementsByClassName(className);
    if (list.isEmpty) {
      return null;
    }
    var eles = list.where((element) => element.localName == tag).toList();
    if (eles.isEmpty) return null;
    return eles;
  }

  dom.Element? loadTagName(String tagName) {
    var list = getElementsByTagName(tagName);
    if (list.isEmpty) {
      return null;
    }
    return list[0];
  }
}

extension MyParseDocument on dom.Document {
  dom.Element? loadClassName(String className) {
    if (className.startsWith(".")) {
      var arr = className.substring(1).split(".");
      var list = getElementsByClassName(arr[0]);
      for (var item in list) {
        var itemClassName = item.className;
        var hadAll = true;
        for (var child in arr) {
          if (!itemClassName.contains(child)) {
            hadAll = false;
            break;
          }
        }
        if (hadAll) {
          return item;
        }
      }
      return null;
    } else {
      var list = getElementsByClassName(className);
      if (list.isEmpty) {
        return null;
      }
      return list[0];
    }
  }

  dom.Element? loadTagClassName(String name) {
    var arr = name.split('.');
    String tag = arr[0];
    String className = arr[1];
    var list = getElementsByClassName(className);
    if (list.isEmpty) {
      return null;
    }
    var ele = list.firstWhere(
      (element) => element.localName == tag,
      orElse: () => dom.Element.tag('empty'),
    );
    if (ele.localName == 'empty') return null;
    return ele;
  }

  List<dom.Element>? loadTagClassNames(String name1) {
    String name = name1.trim();
    if (name.trim().isEmpty) return null;

    if (name.startsWith(".")) {
      return getElementsByClassName(name.substring(1, name.length));
    }
    if (!name.contains(".")) {
      return getElementsByTagName(name);
    }
    var arr = name.split('.');
    String tag = arr[0];
    String className = arr[1];
    var list = getElementsByClassName(className);
    if (list.isEmpty) {
      return null;
    }
    var eles = list.where((element) => element.localName == tag).toList();
    if (eles.isEmpty) return null;
    return eles;
  }

  dom.Element? loadTagName(String tagName) {
    var list = getElementsByTagName(tagName);
    if (list.isEmpty) {
      return null;
    }
    return list[0];
  }
}
